<?php
/**
 * RedCross Application
 *
 */


class Core_Form_Member_Application extends RedCross_Form
{
    public function init()
    {
        $this->addElement('text', 'first_name', array(
            'filters'        => array('StringTrim'),
            'validators'    =>  array(
                array('NotEmpty', true),
                array('Alpha', true, true),
                array('StringLength', true, array(2,32)),
            ),
            'required'     => true,
            'label'     => 'Fornavn'
        ));

        $this->addElement('text', 'last_name', array(
            'filters'        => array('StringTrim'),
            'validators'    =>  array(
                array('NotEmpty', true),
                array('Alpha', true, true),
                array('StringLength', true, array(2,32)),
            ),
            'required'     => true,
            'label'     => 'Etternavn'
        ));

        $this->addElement('text', 'birth_date', array(
            'filters'        => array('StringTrim'),
            'validators'    => array(
                array('NotEmpty', true),
            ),
            'label' => 'Fødselsdato'
        ));

        $this->addElement('text', 'address', array(
            'filters'        => array('StringTrim'),
            'validators'    =>  array(
                array('StringLength', true, array(4,32)),
            ),
            'label'     => 'Adresse'
        ));

        $this->addElement('select', 'postal_code', array(
            'filters'        => array('StringTrim'),
            'validators'    => array(
                array('NotEmpty', true),
                array('Digits', true),
            ),
            'label' => 'Postnummer'
        ));

        $this->getElement('postal_code')->addMultiOption(null, '-- Velg postnummer --');
        
        $this->addElement('text', 'phone', array(
            'filters'        => array('StringTrim'),
            'validators'    => array(
                array('Digits'),
                array('StringLength', true, array(8, 8)),
            ),
            'required'     => true,
            'label'     => 'Telefon'
        ));

        $this->addElement('text', 'email', array(
            'filters'        => array('StringTrim'),
            'validators'    => array(
                array('StringLength', true, array(6, 60)),
                array('EmailAddress', true),
            ),
            'required'     => true,
            'label'     => 'E-post'
        ));

        $this->addDisplayGroup(array('first_name', 'last_name', 'birth_date', 'address', 'postal_code', 'phone', 'email'), 'personalia'); 

        $this->addElement('textarea', 'general_info', array(
            'required' => false,
            'label' => 'Fortell litt om deg selv og hva du driver med',
            'rows' => 6,
            'cols' => 50
        ));

        $this->addElement('textarea', 'motivation', array(
            'required' => false,
            'label' => 'Hvorfor har du lyst å bli med i hjelpekorpset',
            'rows' => 6,
            'cols' => 50
        ));


        $this->addElement('textarea', 'experience', array(
            'required' => false,
            'label' => 'Tidligere relevant erfaring',
            'rows' => 6,
            'cols' => 50
        ));

        $this->addDisplayGroup(array('general_info', 'motivation', 'experience'), 'general');

        $this->addElement('checkbox', 'send_info_mail', array(
            'required' => false,
            'label' => 'Send meg gjerne informasjon på e-post'
        ));
        $this->getElement('send_info_mail')->getDecorator('Label')->setOption('placement', 'APPEND');
        $this->addDisplayGroup(array('send_info_mail'), 'mail_checkbox');

        $this->addElement('submit', 'submit', array(
            'required' => true,
            'ignore'   => true,
            'label'    => 'Send',
        ));

        $this->fillPostalCodes();
    }

    private function fillPostalCodes()
    {
        $postalService = RedCross_Factory::getService('Core_Service_Postal');
        $postalCodes = $postalService->fetchAll();
        $postalElement = $this->getElement('postal_code');
        foreach($postalCodes as $postal) {
            $postalElement->addMultiOption($postal['id'], $this->getPostalCode($postal['id']) . ' - ' . $postal['area']);
        }
    }

    private function getPostalCode($postal_id)
    {
        $length = strlen($postal_id);
        switch($length) {
            case 1:
                return '000' . $postal_id;
                break;
            case 2:
                return '00' . $postal_id;
                break;
            case 3:
                return '0' . $postal_id;
                break;
        }
        return $postal_id;
    }


}
